/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package ant.antsys;

import java.io.File;
import java.io.IOException;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.util.FileUtils;

/**
 * Assertion class to check if two files are equal
 * 
 * @author johnson ma
 */
public class AssertFileEquals extends AssertFail {

	private File expectedFile;
	private File actualFile;

	// The method executing the task
	public void execute() throws BuildException {
		if (expectedFile == null) {
			throw new BuildException(
					"The expectedfile attribute must be present.",
					getLocation());
		}
		if (actualFile == null) {
			throw new BuildException(
					"The actualfile attribute must be present.", getLocation());
		}
		if (!actualFile.exists()) {
			BuildException be = new BuildException("file doesn't exists:"
					+ actualFile);
			throw be;
		}
		if (!expectedFile.exists()) {
			BuildException be = new BuildException("file doesn't exists:"
					+ expectedFile);
			throw be;
		}
		try {
			boolean result = FileUtils.getFileUtils().contentEquals(actualFile,
					expectedFile);
			if (negative) {
				if (result) {
					BuildException be = new BuildException(
							errorMsg != null ? errorMsg
									: "AssertFileEquals: expected not euqal:"
											+ expectedFile + " actual:"
											+ actualFile, getLocation());
					throw be;
				}
			} else {
				if (!result) {
					BuildException be = new BuildException(
							errorMsg != null ? errorMsg
									: "AssertFileEquals: expected not euqal:"
											+ expectedFile + " actual:"
											+ actualFile, getLocation());
					throw be;
				}
			}
		} catch (IOException e) {
			BuildException be = new BuildException(
					"AssertFileEquals: expected:" + expectedFile + " actual:"
							+ actualFile + " error:" + e, getLocation());
			throw be;
		}
	}

	public void setExpectedFile(File expectedFile) {
		this.expectedFile = expectedFile;
	}

	public void setActualFile(File actualFile) {
		this.actualFile = actualFile;
	}

}
